Aug 25, 2023
CharField in a model will usually generate a CharField in the ModelForm.max_length attribute, the corresponding form field will enforce that limit during validation.save() method on a validated ModelForm, it creates a new model instance or updates an existing one, depending on the form’s instance parameter.library app, define a model for a book in the models.py file:forms.py file within the library app, create a ModelForm for the Book model:urls.py file (bookstore/urls.py), include the URLs of the library app:Create views and templates to handle the book creation form:
In the library app directory, create a urls.py file:
library app directory, create a views.py file:# library/views.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from .models import Book
from .forms import BookForm
class BookCreateView(CreateView):
model = Book
form_class = BookForm
template_name = 'library/book_form.html'
success_url = reverse_lazy('book_add')
def form_valid(self, form):
messages.success(self.request, 'Book added successfully!')
return super().form_valid(form)
def home(request):
books = Book.objects.all()
return render(request, 'library/home.html', {'books': books})templates directory within the library app directory, and inside it, create a book_form.html template:<!-- library/templates/library/book_form.html -->
<!DOCTYPE html>
<html>
<head>
<title>Add Book</title>
</head>
<body>
<h1>Add Book</h1>
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Add Book</button>
</form>
</body>
</html><!-- library/templates/library/home.html -->
<!DOCTYPE html>
<html>
<head>
<title>Bookstore Home</title>
</head>
<body>
<h1>Welcome to the Bookstore!</h1>
<ul>
{% for book in books %}
<li>{{ book.title }} by {{ book.author }}</li>
{% endfor %}
</ul>
<a href="{% url 'book_add' %}">Add a Book</a>
</body>
</html># library/admin.py
from django.contrib import admin
from .models import Book
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'publication_date')Don’t forget to use makemigrations and migrate command.
In the above code:
Book model.@admin.register decorator to register the Book model with the admin site.BookAdmin class that inherits from admin.ModelAdmin.list_display attribute to determine what fields are displayed in the list view of the admin site for the Book model.admin.py file, you ensure that the changes made to the Book model and BookForm are reflected in the Django admin site as well.Access the book creation form at http://127.0.0.1:8000/library/add/.
This example demonstrates how to use Django ModelForms to create a form for adding books to your bookstore project.
The form is automatically generated based on the Book model fields defined in models.py.
Manish Patel